#include "Monster.h"
#include "Map.h"

Monster::Monster(int Entry, 
	int maxheath, 
	int Attack_Power, 
	int Defance, 
	float MissChance, 
	float CritChance, 
	int Level, 
	int Loot, 
	int reswardexp, 
	int rewardmoney, 
	std::string imageurl, 
	std::string deadsoundurl,
	std::string takedamagesoundurl) : Unit(TypeID_Monster)
{
	initWithFile("action/monster/moving/monster1_1.png");
	autorelease();
	_BaseValue.Entry				= Entry;
	_BaseValue.maxheath				= maxheath;
	_BaseValue.Attack_Power			= Attack_Power;
	_BaseValue.Defance				= Defance;
	_BaseValue.MissChance			= MissChance;
	_BaseValue.CritChance			= CritChance;
	_BaseValue.Level				= Level;
	_BaseValue.Loot					= Loot;
	_BaseValue.reswardexp			= reswardexp;
	_BaseValue.rewardmoney			= rewardmoney;
	_BaseValue.imageurl				= imageurl;
	_BaseValue.deadsoundurl			= deadsoundurl;
	_BaseValue.takedamagesoundurl	= takedamagesoundurl;
	Heath = maxheath;
	m_target = nullptr;
	m_target_chaeck_timer = 0;
	m_CanWalk = false;
	m_MovingType = false;
	DeadTime = 0;
	m_RandomMovingTimer = 0;
	FallingSpeed = 1.0f;
	scheduleUpdate();
}

Monster::~Monster()
{
	if (getActionByTag(MOVE_ACTION_TAG))
		getActionByTag(MOVE_ACTION_TAG)->release();
}

void Monster::SetTarget(Unit* pTarget, float _CheckTimer)
{
	if (m_target)
		return;
	m_target = pTarget;
	m_target_chaeck_timer = _CheckTimer;
}

CCAnimate* Monster::GetMoving()
{
	CCAnimation* standanimation = new CCAnimation();
	for (int i = 1; i != 8; i++)
	{
		char str[200];
		sprintf(str, "action/monster/moving/monster1_%d.png", i);
		standanimation->addSpriteFrameWithFile(str);
	}
	standanimation->setDelayPerUnit(1.0f / 8.0f);
	standanimation->setRestoreOriginalFrame(true);
	standanimation->setLoops(1);
	CCAnimate* standanimate = CCAnimate::create(standanimation);
	return standanimate;
}

void Monster::GetRandomMoving(bool left)
{
	if (!CanWalk())
		return;

	if (left)
	{
		if (Map_Layer::GetInstance()->CanMoveLeft(this))
		{
			setFlipX(false);
			setPositionX(getPositionX() - 1.0f);
		}
	}
	else if (Map_Layer::GetInstance()->CanMoveRight(this))
	{
		setFlipX(true);
		setPositionX(getPositionX() + 1.0f);
	}

	if (!getActionByTag(MOVE_ACTION_TAG))
	{
		CCRepeatForever* MoveRepeat = CCRepeatForever::create(GetMoving());
		MoveRepeat->setTag(MOVE_ACTION_TAG);
		runAction(MoveRepeat);
	}
}

bool Monster::IsCrachedPlayer()
{
	if (Map_Scene::GetInstance()->GetLoadedPlayer()->IsAlive())
		return (abs(Map_Scene::GetInstance()->GetLoadedPlayer()->getPositionX() - getPositionX()) < getBoundingBox().size.width / 2 && abs(Map_Scene::GetInstance()->GetLoadedPlayer()->getPositionY() - getPositionY()) < getBoundingBox().size.height / 2);
	return false;
}

void Monster::update(float diff)
{
	Unit::update(diff);
	for (size_t i = 0; i < WaitingDeleteNumberLines.size(); i++)
	{
		if (WaitingDeleteNumberLines.at(i)->DelayTime <= diff)
		{
			for (size_t i = 0; i < WaitingDeleteNumberLines.at(i)->Numbers.size(); i++)
			{
				WaitingDeleteNumberLines.at(i)->Numbers.at(i)->removeFromParentAndCleanup(true);
			}
		}
		WaitingDeleteNumberLines.at(i)->DelayTime -= diff;
	}

	if (m_target_chaeck_timer <= diff && m_target)
	{
		m_target = nullptr;
		m_target_chaeck_timer = 0;
	}
	else m_target_chaeck_timer -= diff;

	if (!IsAlive())
		return;

	if (IsCrachedPlayer())
	{
		DealDamage(Map_Scene::GetInstance()->GetLoadedPlayer(), _BaseValue.Attack_Power);
	}
	if (m_RandomMovingTimer >= diff)
	{
		GetRandomMoving(m_MovingType);
		m_RandomMovingTimer -= diff;
	}
	else
	{
		if (m_target)
		{
			m_target->getPositionX() > getPositionX() ? m_MovingType = false : m_MovingType = true;
			m_RandomMovingTimer = 1.0f;
		}
		else
		{
			rand() % 100 > 50 ? m_MovingType = true : m_MovingType = false;
			m_RandomMovingTimer = 3.0f;
		}
	}
}